import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/Login.jsp").forward(req,resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");


        if("root".equals(username) && "123123".equals(password))
        {
            //登录成功
            req.getSession().setAttribute("username",username);
            resp.sendRedirect(req.getContextPath()+"/stu");
        }else{
            //登陆失败
            req.setAttribute("msg","用户名或者密码错误");
            req.getRequestDispatcher("Login.jsp").forward(req,resp);
        }
    }
}
